package com.base.tree;


public class TreeNodeRender {
	
	private int nodeMargin = 20; // 节点缩进距离
	
	private TreeNode node = null;
	
	
	private String childContainerSuffix = "_div";// 子节点容器后缀
	
	public TreeNodeRender(TreeNode node) {
		this.node = node;
	}
	
	/**
	 * 获取点击事件js
	 * @return
	 */
	public StringBuffer getClickEventHTML() {
		StringBuffer eventStr = new StringBuffer();
		return eventStr;
	}
	
	/**
	 * 获取事件js代码
	 * @return
	 */
	public StringBuffer getEventHTML(boolean onlyClick) {
		
		StringBuffer eventStr = new StringBuffer();// 事件
		StringBuffer customArgs = new StringBuffer();// 自定义参数
		String externalArgs[] = null;
		
		if(node.getEventMgr() == null) {
			return eventStr;
		}
		
		for(TreeEvent event: node.getEventMgr().getEvents()){
			if(onlyClick && event.getType().getValue() != TreeEventType.click.getValue()) {
				continue;
			}
			externalArgs = event.getExternalArgs();
			if(externalArgs != null && externalArgs.length > 0){
				customArgs.append(",");
				for (int i = 0; i < externalArgs.length; i++) {
					customArgs.append("\"" + externalArgs[i] + "\"");
					if(i < externalArgs.length - 1){
						customArgs.append(",");
					}
				}
			}
			
			eventStr.append(event.getType()).append("=")
			.append("'")
			.append(TreeEvent.EVENT_DISPATCHER)
			.append("(")
				.append(event.getHandler()).append(",")
				.append(customArgs.toString())
				.append("\"" + node.toArgs().toString() + "\"")
			.append(");")
			.append("' ");
		}
		return eventStr;
	}
	
	/**
	 * 获取节点图片
	 * @return
	 */
	public String getNodeImg() {
		String nodeImg = "";
		
		if(node.isLeaf()) {// 叶子节点
			if("".equals(node.getLeafImg())) {// 默认
				nodeImg = node.getImgHomeDir() + node.getStyle().getValue() + "/" + "leaf.gif";
			}else {
				nodeImg = node.getImgHomeDir() + node.getLeafImg();
			}
		}else { // 文件夹
			if(node.isOpen()){
				if("".equals(node.getFolderOpenImg())) {
					nodeImg = node.getImgHomeDir() + node.getStyle().getValue() + "/" + "folder_open.gif";
				}else {
					nodeImg = node.getImgHomeDir() + node.getFolderOpenImg();
				}
			}else if(node.isSelected()) {
				if("".equals(node.getFolderSelectedImg())) {
					nodeImg = node.getImgHomeDir() + node.getStyle().getValue() + "/" + "folder_selected.gif";
				}else {
					nodeImg = node.getImgHomeDir() + node.getFolderSelectedImg();
				}
			}else {
				if("".equals(node.getFolderImg())) {
					nodeImg = node.getImgHomeDir() + node.getStyle().getValue() + "/" + "folder.gif";
				}else {
					nodeImg = node.getImgHomeDir() + node.getFolderImg();
				}
			}
		}
		
		return nodeImg;
	}
	
	/**
	 * 生成节点的HTML代码
	 * @return
	 */
	public StringBuffer doView() {
		StringBuffer html = new StringBuffer();
		
		if(this.node == null) {
			return html;
		}
		
		// icon打开、关闭小图标
		String iconPath = "";
		if(node.isLeaf()) {
			iconPath = node.getImgHomeDir() + node.getStyle().getValue() + "/" + TreeNode.icon_sub;
		}else {
			iconPath = node.getImgHomeDir() + node.getStyle().getValue() + "/" + TreeNode.icon_plus;
		}
		
		// 文字前小图片
		String nodeImgPath = this.getNodeImg();
		
		
		// 缩进
		String margin = "margin-left:" + ((node.getLevel()  - 1 ) * nodeMargin) + "px;"; 
		String cursor = "cursor:hand;";
		String nodeStyle = margin;
		
		html.append("<table")
							.append(" id='").append(node.getId()).append("'")
		                    .append(" style='").append(nodeStyle).append("'")
							.append(" title='" + node.getTitle() + "'")
					.append(">")
				.append("<tr>");
			   
				// 展开收缩图标
				html.append("<td>");
		        	if(node.isLeaf()) { // 添加点击事件
		        		if(node.isShowLeafIcon()) {
		        			html.append("<img src='").append(iconPath).append("'/>");
		        		}
		        	}else{
		        		html.append("<img src='").append(iconPath).append("'")
		        					.append(" onclick=\"tree.viewChildNodes('" + node.getId() + "', false)\"")
		        					.append("/>");
		        	}
	        	html.append("</td>");
				
	        	// 节点图片
	        	html.append("<td>");
				html.append("<img src='").append(nodeImgPath).append("' ")
				        .append(getEventHTML(true))
						//.append(" onclick=\"tree.viewChildNodes('" + node.getId() + "', true)\"")
						.append("/>");
				html.append("</td>");
					
				// 节点文本
				html.append("<td").append(" style='").append(cursor).append("' ")
								  .append(getEventHTML(false))
						    .append(" nowrap>")
						.append(node.getName())
					.append("</td>");
					
		html.append("</tr>")
		    .append("</table>");
		
		// 添加容器
		if(!node.isLeaf()) {
			html.append("<div")
				.append(" id='").append(node.getId() + childContainerSuffix).append("'")
				.append(">")
			    .append("</div>");
		}
		
		return html;
	}
	
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		TreeNode node = new TreeNode("0001", "第一个节点", false);
		TreeNodeRender render = new TreeNodeRender(node);
		node.setEventMgr(new TreeEventManager());
		node.getEventMgr().registerEvent(new TreeEvent("t1"));
		node.addInfo("guid", "guiid000232adfdsf");
		String html = render.doView().toString();
		System.out.println(html);
	}
}
